package com.ruoyi.patrol.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.patrol.domain.*;
import com.ruoyi.patrol.domain.vo.PtFormDetailVo;
import com.ruoyi.patrol.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import com.ruoyi.patrol.service.IPtFormFillingService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 单填报学校Service业务层处理
 *
 * @author lkm
 * @date 2022-11-10
 */
@Service
public class PtFormFillingServiceImpl implements IPtFormFillingService {
    @Autowired
    private PtFormFillingMapper ptFormFillingMapper;

    @Autowired
    private PtFormMapper ptFormMapper;

    @Autowired
    private PtFormDetailsMapper ptFormDetailsMapper;

    @Autowired
    private PtFormDetailsOptionMapper ptFormDetailsOptionMapper;

    @Autowired
    private PtFormReplyMapper ptFormReplyMapper;

    /**
     * 查询单填报学校
     *
     * @param id 单填报学校ID
     * @return 单填报学校
     */
    @Override
    public PtFormFilling selectPtFormFillingById(Long id) {
        PtFormFilling ptFormFilling = ptFormFillingMapper.selectPtFormFillingById(id);
        PtForm ptForm = ptFormMapper.selectPtFormById(ptFormFilling.getFormId());
        PtFormDetailVo ptFormDetailVo = new PtFormDetailVo();
        ptFormDetailVo.setFormTitle(ptForm.getFormName());
        ptFormDetailVo.setTime(ptFormFilling.getFillTime());
        ptFormDetailVo.setFillinperson(ptFormFilling.getUpdateBy());
//        获取表单回答列表
        PtFormReply ptFormReply = new PtFormReply();
        ptFormReply.setSchoolId(ptFormFilling.getSchoolId());
        ptFormReply.setFormId(ptFormFilling.getFormId());
        List<PtFormReply> ptFormReplies = ptFormReplyMapper.selectPtFormReplyList(ptFormReply);
//        获取表单问题及选项，组合数据回传
        PtFormDetails ptFormDetails = new PtFormDetails();
        ptFormDetails.setFormId(ptFormFilling.getFormId());
        LinkedList<PtFormDetailVo.Formlist> formlists = new LinkedList<>();
        List<PtFormDetails> formDetails = ptFormDetailsMapper.selectPtFormDetailsList(ptFormDetails);
        formDetails.forEach(v -> {
            PtFormDetailVo.Formlist formlist = new PtFormDetailVo.Formlist();
            formlist.setTitle(v.getTitle());
            formlist.setNumber(v.getSerialno().intValue());
            formlist.setType((v.getType() + 1));
            formlist.setKey(v.getId());
            if (v.getType() == 0) {
                PtFormDetailsOption ptFormDetailsOption = new PtFormDetailsOption();
                ptFormDetailsOption.setFromDetailsId(v.getId());
                List<PtFormDetailsOption> ptFormDetailsOptions = ptFormDetailsOptionMapper.selectPtFormDetailsOptionList(ptFormDetailsOption);
                ArrayList<PtFormDetailVo.Formlist.Optionlist> list = new ArrayList<>();
                ptFormDetailsOptions.forEach(y -> {
                    PtFormDetailVo.Formlist.Optionlist optionlist = new PtFormDetailVo.Formlist.Optionlist();
                    optionlist.setKey(y.getId());
                    optionlist.setOption(y.getFormDetailsOptionName());
                    optionlist.setReason(y.getReason() == 1);
                    list.add(optionlist);
                });
                formlist.setOptionlist(list);
            }
            formlists.add(formlist);
        });
        formlists.forEach(v -> {
            for (int i = 0; i < ptFormReplies.size(); i++) {
                PtFormReply reply = ptFormReplies.get(i);
                if (reply.getFromDetailsId() == v.getKey()) {
                    if (v.getType() == 1) {
                        v.getOptionlist().forEach(y -> {
                            //分割选项及回答原因
                            String[] split1 = reply.getReply().split("#");
                            String[] split2 = reply.getReplyReason().split("#");
                            for (int i1 = 0; i1 < split1.length; i1++) {
                                if (split1[i1].equals(y.getKey() + "")) {
                                    y.setChecked(true);
                                    if (y.isReason()) {
                                        y.setReasontext(split2[i1]);
                                    }
                                }
                            }
                        });
                    } else {
                        v.setReply(reply.getReply());
                    }
                    break;
                }
            }
        });
        ptFormDetailVo.setFormlist(formlists);
        ptFormFilling.setPtFormDetailVo(ptFormDetailVo);
        return ptFormFilling;
    }

    @Override
    public List<PtFormFilling> selectSinglePtFormFillingList(PtFormFilling ptFormFilling) {
        return ptFormFillingMapper.selectSinglePtFormFillingList(ptFormFilling);
    }

    /**
     * 查询单填报学校列表
     *
     * @param ptFormFilling 单填报学校
     * @return 单填报学校
     */
    @Override
    public List<PtFormFilling> selectPtFormFillingList(PtFormFilling ptFormFilling) {
        return ptFormFillingMapper.selectPtFormFillingList(ptFormFilling);
    }

    /**
     * 新增单填报学校
     *
     * @param schools 单填报学校
     * @param formid  单填报表单id
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPtFormFilling(List<PtFormDetailVo.Schools> schools, Long formid) {
        List<PtFormFilling> ptFormFillings = new ArrayList<>();
        Date nowDate = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        schools.forEach((itme) -> {
            PtFormFilling ptFormFilling = new PtFormFilling();
            ptFormFilling.setCreateBy(username);
            ptFormFilling.setCreateTime(nowDate);
            ptFormFilling.setSchoolId(itme.getKey());
            ptFormFilling.setFormId(formid);
            ptFormFilling.setFillType(0L);
            ptFormFilling.setFillTimeLimit(itme.getTime());
            ptFormFillings.add(ptFormFilling);
        });
        return ptFormFillingMapper.insertPtFormFilling(ptFormFillings);
    }

    /**
     * 修改单填报学校
     *
     * @param ptFormFilling 单填报学校
     * @return 结果
     */
    @Override
    public int updatePtFormFilling(PtFormFilling ptFormFilling) {
        ptFormFilling.setUpdateTime(DateUtils.getNowDate());
        return ptFormFillingMapper.updatePtFormFilling(ptFormFilling);
    }

    /**
     * 批量删除单填报学校
     *
     * @param ids 需要删除的单填报学校ID
     * @return 结果
     */
    @Override
    public int deletePtFormFillingByIds(Long[] ids) {
        return ptFormFillingMapper.deletePtFormFillingByIds(ids);
    }

    /**
     * 删除单填报学校信息
     *
     * @param id 单填报学校ID
     * @return 结果
     */
    @Override
    public int deletePtFormFillingById(Long id) {
        return ptFormFillingMapper.deletePtFormFillingById(id);
    }
}
